package gui_classes;

import java_persistence.PatientMapper;
import java_persistence.UserMapper;

import javax.swing.*;

import org.apache.ibatis.session.SqlSession;

import domain.Patient;
import domain.User;

import java.util.List;


import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Date;

/*
 * Panel for registering new patients
 */
public class RegistrationPanel extends DERPBase {
	private JTextField txtUsername;
	private JTextField txtPassword;
	private JTextField txtName;
	private JTextField txtLastName;
	private JTextField txtEmail;
	private JTextField txtPhoneNumber;
	private JTextField txtAddressLine;
	private JTextField txtGender;
	
	private String[] arr;
	private JTextField txtAllergies;
	private JTextField txtInsuranceInfo;
	private JTextField txtPharmacy;
	
	private JButton btnNext;
	private JTextField txtYyyy;
	private JTextField txtMm;
	private JTextField txtDd;
	

	

	/**
	 * Create the panel.
	 */
	public RegistrationPanel() {
		setPreferredSize(new Dimension(600, 400));
		setLayout(null);
		
		GUI_Main.getFrame().setTitle("New Patient Registration");
		
		arr = new String[11];
		
		JLabel lblDesiredUsername = new JLabel("Desired Username:");
		lblDesiredUsername.setBounds(12, 26, 142, 15);
		add(lblDesiredUsername);
		
		txtUsername = new JTextField();
		txtUsername.setText("Username");
		txtUsername.setBounds(154, 24, 114, 19);
		add(txtUsername);
		txtUsername.setColumns(10);
		
		txtPassword = new JTextField();
		txtPassword.setText("Password");
		txtPassword.setBounds(153, 62, 114, 19);
		add(txtPassword);
		txtPassword.setColumns(10);
		
		JLabel lblDesiredPassword = new JLabel("Desired Password:");
		lblDesiredPassword.setBounds(12, 64, 142, 15);
		add(lblDesiredPassword);
		
		JLabel lblName = new JLabel("First Name:");
		lblName.setBounds(300, 26, 92, 15);
		add(lblName);
		
		txtName = new JTextField();
		txtName.setText("First Name");
		txtName.setBounds(386, 24, 114, 19);
		add(txtName);
		txtName.setColumns(10);
		
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setBounds(300, 66, 92, 15);
		add(lblLastName);
		
		txtLastName = new JTextField();
		txtLastName.setText("Last Name");
		txtLastName.setBounds(386, 66, 114, 19);
		add(txtLastName);
		txtLastName.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(12, 192, 70, 15);
		add(lblEmail);
		
		txtEmail = new JTextField();
		txtEmail.setText("email");
		txtEmail.setBounds(67, 190, 201, 19);
		add(txtEmail);
		txtEmail.setColumns(10);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(300, 192, 70, 15);
		add(lblPhone);
		
		txtPhoneNumber = new JTextField();
		txtPhoneNumber.setText("Phone Number");
		txtPhoneNumber.setBounds(355, 190, 114, 19);
		add(txtPhoneNumber);
		txtPhoneNumber.setColumns(10);
		
		JLabel lblAddressLine = new JLabel("Address:");
		lblAddressLine.setBounds(12, 234, 114, 15);
		add(lblAddressLine);
		
		txtAddressLine = new JTextField();
		txtAddressLine.setText("Address:");
		txtAddressLine.setBounds(87, 232, 468, 19);
		add(txtAddressLine);
		txtAddressLine.setColumns(10);
		
		btnNext = new JButton("Register");
		btnNext.setBounds(228, 363, 117, 25);
		btnNext.addActionListener(new RegisterListener());
		add(btnNext);
		
		JLabel lblGender = new JLabel("Gender(M/F):");
		lblGender.setBounds(12, 108, 92, 15);
		add(lblGender);
		
		JLabel lblBirthdate = new JLabel("Birthdate:");
		lblBirthdate.setBounds(216, 108, 82, 15);
		add(lblBirthdate);
		
		
		
		JLabel lblAllergiesseparateBy = new JLabel("Allergies (Separate by commas):");
		lblAllergiesseparateBy.setBounds(12, 156, 229, 15);
		add(lblAllergiesseparateBy);
		
		txtAllergies = new JTextField();
		txtAllergies.setText("Allergies");
		txtAllergies.setBounds(259, 154, 329, 19);
		add(txtAllergies);
		txtAllergies.setColumns(10);
		
		JLabel lblInsuranceInfo = new JLabel("Insurance Info:");
		lblInsuranceInfo.setBounds(12, 274, 114, 15);
		add(lblInsuranceInfo);
		
		txtInsuranceInfo = new JTextField();
		txtInsuranceInfo.setText("Insurance Info");
		txtInsuranceInfo.setBounds(125, 272, 463, 19);
		add(txtInsuranceInfo);
		txtInsuranceInfo.setColumns(10);
		
		JLabel lblPharmacy = new JLabel("Pharmacy:");
		lblPharmacy.setBounds(12, 320, 92, 15);
		add(lblPharmacy);
		
		txtPharmacy = new JTextField();
		txtPharmacy.setText("Pharmacy");
		txtPharmacy.setBounds(103, 318, 485, 19);
		add(txtPharmacy);
		txtPharmacy.setColumns(10);
		
		txtGender = new JTextField();
		txtGender.setText("M/F");
		txtGender.setBounds(118, 106, 63, 19);
		add(txtGender);
		txtGender.setColumns(10);
		
		txtYyyy = new JTextField();
		txtYyyy.setText("YYYY");
		txtYyyy.setBounds(336, 106, 43, 19);
		add(txtYyyy);
		txtYyyy.setColumns(4);
		
		txtMm = new JTextField();
		txtMm.setText("MM");
		txtMm.setBounds(466, 106, 26, 19);
		add(txtMm);
		txtMm.setColumns(2);
		
		txtDd = new JTextField();
		txtDd.setText("DD");
		txtDd.setBounds(541, 106, 35, 19);
		add(txtDd);
		txtDd.setColumns(2);
		
		JLabel lblYear = new JLabel("Year:");
		lblYear.setBounds(300, 108, 43, 15);
		add(lblYear);
		
		JLabel lblMonth = new JLabel("Month:");
		lblMonth.setBounds(415, 108, 54, 15);
		add(lblMonth);
		
		JLabel lblDay = new JLabel("Day:");
		lblDay.setBounds(510, 108, 70, 15);
		add(lblDay);
						
	}
	
	public void display()
	{
		GUI_Main.replacePanel(this);
	}

	/**
	 * Getter for the username field
	 * @return txtUsername
	 */
	public JTextField getUsername()
	{
		return this.txtUsername;
	}
	
	/**
	 * Getter for password field
	 * @return txtPassword
	 */
	public  JTextField getPassword()
	{
		return this.txtPassword;
	}
	
	/**
	 * getter for First name
	 * @return txtName
	 */
	public  JTextField getNameF()
	{
		return this.txtName;
	}
	
	/**
	 * getter Last name
	 * @return txtlastName
	 */
	public  JTextField getLName()
	{
		return this.txtLastName;
	}
	
	/**
	 * getter for email field
	 * @return txtEmail
	 */
	public  JTextField getTextField()
	{
		return this.txtEmail;
	}
	
	/**
	 * getter for phone n. field
	 * @return txtPhoneNumber
	 */
	public  JTextField getPhN()
	{
		return this.txtPhoneNumber;
	}
	public  JTextField getAddr()
	{
		return this.txtAddressLine;
	}
	
	public  String[] getArray()
	{
		return this.arr;
	}
	public  JTextField getTextField1()
	{
		return this.txtAllergies;
	}
	public  JTextField getTextField2()
	{
		return this.txtInsuranceInfo;
	}
	
	public  JTextField getTextField3()
	{
		return this.txtPharmacy;
	}
	
	public void setArray(String[] array)
	{
		this.arr = array;
	}
	
	public JTextField getTxtGender()
	{
		return this.txtGender;
	}
	
	public JTextField getYear()
	{
		return this.txtYyyy;
	}
	
	public JTextField getMonth()
	{
		return this.txtMm;
	}
	
	public JTextField getDay()
	{
		return this.txtDd;
	}
	
	public void changeButLabel(String name)
	{
		this.btnNext.setText(name);
	}

	public void removeButton()
	{
		this.remove(this.btnNext);
	}
	
	private class RegisterListener implements ActionListener {
		public void actionPerformed(ActionEvent event){
			System.out.println("Hit Next");
			
			//have data checks right here
			
			// check to see if username is already used or not.

				
			String[] myArr = new String[11];
			
			try
			{
				myArr[0] = GUI_Main.rpage.getUsername().getText() ; //username
				myArr[1] = GUI_Main.rpage.getPassword().getText(); //password
				String fName = GUI_Main.rpage.getNameF().getText() + " " + GUI_Main.rpage.getLName().getText(); 
				myArr[2] = fName; //Name
				myArr[3] = GUI_Main.rpage.getPhN().getText(); //phone number
				myArr[4] = GUI_Main.rpage.getAddr().getText(); //address
				myArr[5] = GUI_Main.rpage.getTextField().getText(); //email
				myArr[6] = GUI_Main.rpage.getTxtGender().getText(); // gender
				String bdate = GUI_Main.rpage.getYear().getText() + "-" + GUI_Main.rpage.getMonth().getText() + "-" + GUI_Main.rpage.getDay().getText();
				myArr[7] = bdate; // birthdate
				myArr[8] = GUI_Main.rpage.getTextField1().getText(); //allergies
				myArr[9] = GUI_Main.rpage.getTextField2().getText(); // insurance
				myArr[10] = GUI_Main.rpage.getTextField3().getText(); // pharmacy
				
				GUI_Main.rpage.setArray(myArr);
				
				// Print the attributes of the new patient
				for(int x = 0;x<11;x++)
				{
					System.out.println(GUI_Main.rpage.getArray()[x]);
				}
			
		
				/* Joseph:  commit new user to database ( if there is no problem within registration info
			 	 * GUI_Main.rpage.getArray()[0] has username.. and so on.
				 * */

				/* Pratik: is this supposed to be a user or a patient?  You have all the information for a patient, but
				 * I don't want to have to trace your code to figure it out.
				 * 
				 * Answer: this is supposed to be a patient. sorry for the confusion.
				 * 
				 * Pratik: Gender is supposed to be a boolean.  the actual field is called "is male".  
				 * I don't know how you're setting that value, so I'm assuming it's either M or not M
				 * and changing it accordingly.  Give me details or fix it if that's an incorrect assumption. 
				 * 
				 * Joseph: Your assumption is correct. However, for some reason its giving me an exception when
				 * I try to register myself using random data that I made up. Try registering a new user.
				 * You will get this exception:
				 * ### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Memorial, Uninsured, Good music, intelligence, false, 1988-01-23)' at line 2
				 *
				 * Pratik: did I update this to working?  I thought I did, but I forgot to make a note of it.
				 *
				 */
				
				Patient new_patient = new Patient();
				new_patient.setUsername(myArr[0]);
				new_patient.setPassword(myArr[1]);
				new_patient.setName(myArr[2]);
				new_patient.setPhone_number(myArr[3]);
				new_patient.setAddress(myArr[4]);
				new_patient.setEmail(myArr[5]);
				new_patient.setIs_male( (myArr[6]=="M")? true: false ); //If the assumption stated above is correct, this construction does work.
				new_patient.setBirthdate(myArr[7]);
				new_patient.setAllergies(myArr[8]);
				new_patient.setInsurance_info(myArr[9]);
				new_patient.setPharmacy(myArr[10]);
				new_patient.setActive_account(true);
				
				System.out.println("Created Patient object");
				
				SqlSession session = GUI_Main.getSession();
				
				new_patient.create(session.getMapper(UserMapper.class));
				
				System.out.println("Created User.");
				
				new_patient.create_patient(session.getMapper(PatientMapper.class));
				
				System.out.println("Created Patient");
				
				
				session.commit();
				
				System.out.println("Added new patient to database");
				
				
				
				GUI_Main.replacePanel(GUI_Main.lpanel);
			}	
			catch( Exception e ) {
				System.out.println(e.getMessage());
				JOptionPane.showMessageDialog(null, "Database Error: Please see console/log.");
			}
				
		}
				
			
			
			
	} 
}



